package net.bwie.reatime.jtp.controller;

import com.alibaba.fastjson.JSON;
import net.bwie.reatime.jtp.bean.*;
import net.bwie.reatime.jtp.mapper.TrafficChannelStatsService;
import net.bwie.reatime.jtp.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/jtp/traffic")
public class TrafficController {
    @Autowired
    private TrafficChannelStatsService trafficChannelStatsService;
    private static final String DATE_DEFAULT_VALUE = "1970-07-01";
    @RequestMapping("/channe/am")
    public String getChannelPv(@RequestParam  (value = "date",defaultValue = DATE_DEFAULT_VALUE) String date) {
        if (date.equals(DATE_DEFAULT_VALUE)){
            date = DateUtil.nowDate();
        }
        List<TrafficControllerAmBean> trafficChannelAmBeans = trafficChannelStatsService.selectAmCount(date);
        if (trafficChannelAmBeans==null||trafficChannelAmBeans.isEmpty()){
            return "[]";
        }
        return JSON.toJSONString(trafficChannelAmBeans);
    }

    @RequestMapping("/channe/bc")
    public String getChannelBc(@RequestParam  (value = "date",defaultValue = DATE_DEFAULT_VALUE) String date) {
        if (date.equals(DATE_DEFAULT_VALUE)){
            date = DateUtil.nowDate();
        }
        List<TrafficControllerBcBean> trafficcontrollerbcbean = trafficChannelStatsService.selectBcCount(date);
        if (trafficcontrollerbcbean==null||trafficcontrollerbcbean.isEmpty()){
            return "[]";
        }

        return JSON.toJSONString(trafficcontrollerbcbean);
    }
    @RequestMapping("/channe/ct")
    public String getChannelCt(@RequestParam  (value = "date",defaultValue = DATE_DEFAULT_VALUE) String date) {
        if (date.equals(DATE_DEFAULT_VALUE)){
            date = DateUtil.nowDate();
        }
        // 获取Ct
        List<TrafficControllerCtBean> trafficcontrollerctbean = trafficChannelStatsService.selectCtCount(date);
        if (trafficcontrollerctbean==null||trafficcontrollerctbean.isEmpty()){
            return "[]";
        }

        return JSON.toJSONString(trafficcontrollerctbean);
    }
    @RequestMapping("/channe/dt")
    public String getChannelDt(@RequestParam  (value = "date",defaultValue = DATE_DEFAULT_VALUE) String date) {
        if (date.equals(DATE_DEFAULT_VALUE)){
            date = DateUtil.nowDate();
        }
        // 获取Dt
        List<TrafficControllerDtBean> trafficcontrollerdtbean = trafficChannelStatsService.selectDtCount(date);
        if (trafficcontrollerdtbean==null||trafficcontrollerdtbean.isEmpty()){
            return "[]";
        }
        return JSON.toJSONString(trafficcontrollerdtbean);
    }
    @RequestMapping("/channe/ec")
    public String getChannelEc(@RequestParam  (value = "date",defaultValue = DATE_DEFAULT_VALUE) String date) {
        if (date.equals(DATE_DEFAULT_VALUE)){
            date = DateUtil.nowDate();
        }
        // 获取Ec
        List<TrafficControllerEcBean> trafficcontrollerecbean = trafficChannelStatsService.selectEcCount(date);
        if (trafficcontrollerecbean==null||trafficcontrollerecbean.isEmpty()){
            return "[]";
        }
        return JSON.toJSONString(trafficcontrollerecbean);
    }
    @RequestMapping("/channe/ge")
    public String getChannelGe(@RequestParam  (value = "date",defaultValue = DATE_DEFAULT_VALUE) String date) {
        if (date.equals(DATE_DEFAULT_VALUE)){
            date = DateUtil.nowDate();
        }
        // 获取Ec
        List<TrafficControllerGeBean> trafficcontrollergebean = trafficChannelStatsService.selectGeCount(date);
        if (trafficcontrollergebean==null||trafficcontrollergebean.isEmpty()){
            return "[]";
        }
        return JSON.toJSONString(trafficcontrollergebean);
    }
    @RequestMapping("/channe")
    public String getChannel(@RequestParam  (value = "date",defaultValue = DATE_DEFAULT_VALUE) String date) {
        // 获取am
        if (date.equals(DATE_DEFAULT_VALUE)){
            date = DateUtil.nowDate();
        }
        List<TrafficControllerAmBean> trafficchannelambeans = trafficChannelStatsService.selectAmCount(date);
        if (trafficchannelambeans==null||trafficchannelambeans.isEmpty()){
            return "[]";
        }
        // 获取Bc
        List<TrafficControllerBcBean> trafficcontrollerbcbean = trafficChannelStatsService.selectBcCount(date);
        if (trafficcontrollerbcbean==null||trafficcontrollerbcbean.isEmpty()){
            return "[]";
        }
        // 获取Ct
        List<TrafficControllerCtBean> trafficcontrollerctbean = trafficChannelStatsService.selectCtCount(date);
        if (trafficcontrollerctbean==null||trafficcontrollerctbean.isEmpty()){
            return "[]";
        }
        // 获取Dt
        List<TrafficControllerDtBean> trafficcontrollerdtbean = trafficChannelStatsService.selectDtCount(date);
        if (trafficcontrollerdtbean==null||trafficcontrollerdtbean.isEmpty()){
            return "[]";
        }
        // 获取Ec
        List<TrafficControllerEcBean> trafficcontrollerecbean = trafficChannelStatsService.selectEcCount(date);
        if (trafficcontrollerecbean==null||trafficcontrollerecbean.isEmpty()){
            return "[]";
        }

        return "实时GMV"+JSON.toJSONString(trafficchannelambeans)+"，\n实时订单量"+JSON.toJSONString(trafficcontrollerbcbean)+
                "，\n用户参与数"+JSON.toJSONString(trafficcontrollerctbean)+"，\n热门商品TOP100"+JSON.toJSONString(trafficcontrollerdtbean)
                +"，\n地区消费统计"+JSON.toJSONString(trafficcontrollerecbean);
    }
 }
